export const generateProductComb = (arrays: string | any[]) => {
  const results: any[] = [];

  function generateCombinations(current: {}, index: number) {
      if (index === arrays.length) {
          results.push(current);
          return;
      }

      const currentArray = arrays[index];
      for (const value of currentArray.checkList) {
          const newCurrent = { 
            ...current, 
            [currentArray.key]: value,
            code: '',
            price: 1,
            stock: 0
          };
          generateCombinations(newCurrent, index + 1);
      }
  }

  generateCombinations({}, 0);
  return results;
}

/**
 * 获取上传图片的url
 */
export const getUrl = (file:any) => {
  return file.response ? file.response.data : file.url
}

/**
 * 获取图片后缀名
 */
export const getSuffix = (url: string) => {
  const index = url.indexOf('?')
  return url.substring(url.lastIndexOf(".")+1, index);
}

/**
 * 提交时的数据转换
 * 原因：由于后端接口与界面配置不一致，所以需要进行数据转换
 */
export const transformToSubmit = (data: any, spuCategoryAttrList: any[]) => {  
  const hasSku = spuCategoryAttrList && spuCategoryAttrList.length > 0

  const spuAttrList = spuCategoryAttrList.map(spu => {
    return {
      attrName: spu.attrName,
      attrValueList: data[`attrCheckList${spu.spuCategoryAttrId}`]
    }
  })

  const getPaymentModeList = () => {
    const res = [...data.paymentModeList]
    if(res.includes("wx") && res.includes("points")) {
      res.push("wx.points")
    }
    return res
  }

  return {
    categoryCode: data.category.categoryCode,
    spuName: data.spuName,
    spuUnit: data.spuUnit,
    spuDescription: data.spuDescription,
    spuPhoto: getUrl(data.spuPhoto[0]),
    spuBrand: data.spuBrand,
    spuAlbum: data.spuAlbum.map((_:any) => getUrl(_)),
    spuDescAlbum: data.spuDescAlbum.map((_:any) => getUrl(_)),
    spuAttrList: hasSku ? spuAttrList : [],
    skuList: hasSku ? data.tableData.map((sku: any) => ({
      skuId: sku.skuId,
      skuCode: sku.code,
      skuPrice: sku.price,
      skuStock: sku.stock,
      skuAttrList: spuCategoryAttrList.map(spu => {
        return {
          attrName: spu.attrName,
          attrValue: sku[`attr${spu.spuCategoryAttrId}`]
        }
      })
    })) : [{
      skuId: data.skuId,
      skuCode: data.skuCode,
      skuPrice: data.skuPrice,
      skuStock: data.skuStock,
      skuAttrList: []
    }],
    paymentModeList: getPaymentModeList()
  }
}

/**
 * 数据回显的数据转换
 */
export const transformToEdit = (data: any, spuCategoryAttrList: any[]) => {
  if(!data) return {} 
  const hasSku = spuCategoryAttrList && spuCategoryAttrList.length > 0
  
  const item:any = {
    category: data.category,
    spuPhoto: [{
      name: `商品图片.${getSuffix(data.spuPhoto)}`,
      url: data.spuPhoto
    }],
    spuName: data.spuName,
    spuUnit: data.spuUnit,
    spuBrand: data.spuBrand,
    spuDescription: data.spuDescription,
    skuPrice: data.skuPrice,
    spuAlbum: data.spuAlbum.map((item: string, index: number) => ({
      name: `商品轮播图${index + 1}.${getSuffix(item)}`,
      url: item
    })),
    spuDescAlbum: data.spuDescAlbum.map((item: string, index: number) => ({
      name: `商品详情图${index + 1}.${getSuffix(item)}`,
      url: item
    })),
    paymentModeList: data.paymentModeList.filter((i: string) => i !== "wx.points")
  }
  if(!hasSku) {
    item.skuId = data.skuList[0].skuId
    item.skuCode = data.skuList[0].skuCode
    item.skuStock = data.skuList[0].skuStock
  } else {
    // 商品属性
    spuCategoryAttrList.forEach(spu => {
      item[`attrCheckList${spu.spuCategoryAttrId}`] = data.spuAttrList.find((_:any) => _.attrName === spu.attrName).attrValueList
    })
    item.tableData = data.skuList.map((_: any)=> {
      const rowItem: any = {
        skuId:_.skuId,
        stock: _.skuStock,
        code: _.skuCode,
        price: _.skuPrice,
      }
      spuCategoryAttrList.forEach(spu => {
        rowItem[`attr${spu.spuCategoryAttrId}`] = _.skuAttrList.find((item: any) => item.attrName === spu.attrName).attrValue
      })
      return rowItem
    })
  }
  return item
}
